import { defineStore } from "pinia";
import {ref} from 'vue'


export const useUserPermissionStore = defineStore('userInfo', () => {
    const permission = ref({})
    const userInfo = ref({})
    
    const setUserInfo = (newInfo) => {
        userInfo.value = newInfo
    }
    const setPermission = (newInfo) => {
        permission.value = newInfo
    }
    
    const removePermission = () => {
        permission.value = {}
    }
    
    const hasPermission = (requiredRole) => {
  // 确保permission.value和roles存在
  if (!permission.value?.roles) return false
  return permission.value.roles.includes(requiredRole)
}
    
    return { 
        userInfo, 
        setUserInfo,
        permission, 
        setPermission, 
        removePermission,
        hasPermission // 暴露新方法
    }
}, { persist: true })
